#include<bits/stdc++.h>
using namespace std;
#define int long long 
typedef vector<vector<int>> vee;
typedef vector<int> ve;
const int inf=0x3f3f3f3f;

int slove(ve a)
{
    int n=a.size();
    int sum=a.back();
    // int index=1;
    int ret=inf;
    for(int i=1;i<n;i++)
    {
        int diff=sum-a[i]-a[i];
        ret=min(ret,abs(diff));
    }
    return ret;
}

signed main()
{
    int n,m;cin>>n>>m;
    vee a(n+1,vector<int>(m+1));
    ve row(n+1);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
            row[i]+=a[i][j];
        }
    }
    // ve row(n+1);//每一行的和
    ve col(m+1);//每一列的和
    for(int j=1;j<=m;j++)
    {
        for(int i=1;i<=n;i++)
        {
           col[j]+=a[i][j]; 
        }
    }
    //枚举分割线
    ve rowdp(n+1);
    for(int i=1;i<=n;i++)
    rowdp[i]=row[i]+row[i-1];
    ve coldp(m+1);
    for(int j=1;j<=m;j++)
    coldp[j]=col[j]+col[j-1];
    
    int ret1=slove(rowdp);
    int ret2=slove(coldp);
    cout<<min(ret1,ret2)<<endl;
    return 0;
}